package timbap.visualizer.api.context.recorditems;

import timbap.visualizer.api.context.playlist.PlaylistManager;
import timbap.visualizer.api.model.PlaylistEntry;
import timbap.visualizer.api.model.RecordItem;

import java.util.ArrayList;
import java.util.List;

/**
 * The <code>AbstractRecordItemManager</code> is the common
 * base implementation for all record item managers.
 *
 * It initializes the record items from the current playlist
 * and provides methods to update and retrieve selection
 * and record item data.
 *
 * @author  Andreas Pabst
 * @see     timbap.visualizer.api.context.recorditems.RecordItemManager
 * @see     timbap.visualizer.api.model.RecordItem
 */
public abstract class AbstractRecordItemManager implements RecordItemManager {
	
	List<RecordItem> recordItems;
	private int itemCount;
	private int selectedItemIndex;

	public AbstractRecordItemManager(int pItemCount) {
		itemCount = pItemCount;
		recordItems = new ArrayList<RecordItem>(pItemCount);
	}
	
	public void initializeRecordItems(PlaylistManager playlistManager) {
		for (int i = 0; i < getRecordItemCount(); i++) {
            recordItems.add(new RecordItem(playlistManager.getPlaylistEntry(i), "Record Item " + i));
        }
	}

	public List<RecordItem> getRecordItems() {
		return recordItems;
	}
	
	public RecordItem getRecordItemAt(int i) {
		return recordItems.get(i);
	}

	public void setRecordItemAt(int i, RecordItem pRecordItem) {
		recordItems.set(i, pRecordItem);
	}

	public int getRecordItemCount() {
		return itemCount;
	}

	public RecordItem getSelectedRecordItem() {
		return getRecordItemAt(selectedItemIndex);
	}

	public void setSelectedRecordItemIndex(int pSelectedItemIndex) {
		getSelectedRecordItem().setSelected(false);
		getRecordItemAt(pSelectedItemIndex).setSelected(true);
		selectedItemIndex = pSelectedItemIndex;
	}

	public int getSelectedRecordItemIndex() {
		return selectedItemIndex;
	}

	public void setPlaylistEntry(RecordItem pRecordItem, PlaylistEntry pPlaylistEntry) {
		pRecordItem.setPlaylistEntry(pPlaylistEntry);
	}
	
	public void setPlaylistEntries(List<PlaylistEntry> pPlaylistEntries) {
		for (int i=0; i < recordItems.size(); i++) {
			recordItems.get(i).setPlaylistEntry(pPlaylistEntries.get(i));
		}
	}

}
